setwd("E://OneDrive//研究//3-小实验TU//5-论文//入侵程度//数据与分析")

library(ggplot2)
library(ggpubr)

F1_data <- data.frame(x = seq(0,100,0.01))
F1_data$y11 <- 1.22^(F1_data$x/10)-1
F1_data$y12 <- 1.3^(10-(F1_data$x/10))-1
F1_data$y1 <- F1_data$y11 + F1_data$y12
F1_data$y21 <- 1.3^(F1_data$x/10)-1
F1_data$y22 <- 1.22^(10-(F1_data$x/10))-1
F1_data$y2 <- F1_data$y21 + F1_data$y22


F11 <- ggplot(F1_data)+
  geom_line(aes(x=x,y=y11),color="#fdb462", size = 1.5)+
  geom_line(aes(x=x,y=y12),color="#8dd3c7", size = 1.5)+
  geom_line(aes(x=x,y=y1),color="#bebada", size = 1.5)+
  geom_point(x = F1_data$x[F1_data$y1 == min(F1_data$y1)], 
             y = F1_data$y1[F1_data$y1 == min(F1_data$y1)],
             size = 3)+
  geom_segment(x = F1_data$x[F1_data$y1 == min(F1_data$y1)],
            xend = F1_data$x[F1_data$y1 == min(F1_data$y1)],
            y = -1,
            yend = F1_data$y1[F1_data$y1 == min(F1_data$y1)],
            size = 1)+
  geom_text(x = F1_data$x[F1_data$y1 == min(F1_data$y1)], 
            y = F1_data$y1[F1_data$y1 == min(F1_data$y1)],
            label = paste(F1_data$x[F1_data$y1 == min(F1_data$y1)], "%"), 
            vjust = -1.2, size = 6)+
  labs(y = "Competitive intensity", x = "Degree of invasion (%)")+
  theme_classic()+
  theme(axis.title = element_text(size = 14, face = "bold"),
        axis.text.x = element_text(size = 12, face = "bold"),
        axis.text.y = element_blank())

F12 <- ggplot(F1_data)+
  geom_line(aes(x=x,y=y21),color="#fdb462", size = 1.5)+
  geom_line(aes(x=x,y=y22),color="#8dd3c7", size = 1.5)+
  geom_line(aes(x=x,y=y2),color="#bebada", size = 1.5)+
  geom_point(x = F1_data$x[F1_data$y2 == min(F1_data$y2)], 
             y = F1_data$y2[F1_data$y2 == min(F1_data$y2)],
             size = 3)+
  geom_segment(x = F1_data$x[F1_data$y2 == min(F1_data$y2)],
               xend = F1_data$x[F1_data$y2 == min(F1_data$y2)],
               y = -1,
               yend = F1_data$y2[F1_data$y2 == min(F1_data$y2)],
               size = 1)+
  geom_text(x = F1_data$x[F1_data$y2 == min(F1_data$y2)], 
            y = F1_data$y2[F1_data$y2 == min(F1_data$y2)],
            label = paste(F1_data$x[F1_data$y2 == min(F1_data$y2)], "%"), 
            vjust = -1.2, size = 6)+
  labs(y = "Competitive intensity", x = "Degree of invasion (%)")+
  theme_classic()+
  theme(axis.title = element_text(size = 14, face = "bold"),
        axis.text.x = element_text(size = 12, face = "bold"),
        axis.text.y = element_blank())

F1_legend_data <- data.frame(x = rep(1,6),
                             y = rep(1,6),
                             type = rep(c("Intraspecific", "Interspecific", "Total"),2))
F1_legend <- cowplot::get_legend(
  ggplot(F1_legend_data)+
    geom_line(aes(x = x, y = y, color = type), size = 1.5)+
    scale_color_manual(values = c("#fdb462", "#8dd3c7", "#bebada"), 
                       breaks = c("Intraspecific", "Interspecific", "Total"),   
                       labels = c("Intraspecific", "Interspecific", "Total"))+
    theme_bw()+
    theme(legend.position = "top",
          legend.title=element_blank(),
          legend.text = element_text(size = 14, face = "bold"))
)

F1 <- annotate_figure(
  ggarrange(F12, F11, align = "hv"),
  top = F1_legend)

FGA <- annotate_figure(
  ggarrange(F12, F11, align = "hv", ncol = 1),
  top = F1_legend)

ggsave(file = "F1.png", plot = F1, width = 6, height = 3,  bg = "white")
ggsave(file = "FGA.png", plot = FGA, width = 4.5, height = 6,  bg = "white")
